package org.tang.springcloudvv.auth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.tang.springcloudvv.auth.service.UserDetailsServiceImpl;
import org.tang.springcloudvv.util.TokenUtils;
import org.tang.springcloudvv.vo.UserData;

import javax.security.sasl.AuthenticationException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p><em>Created on 2022/3/28 3:13 下午</em></p>
 *
 * @author fuzhongtang
 * @since 1.0.1
 */
@RestController
public class UserTokenTestController {

    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsServiceImpl userDetailsServiceImpl;


    /**
     * postman测试查询用户接口
     * 先调用登录接口获取token
     * 调用本接口：在header增加参数
     * Authorization 值 Bearer 空格加token
     * 验证：测试成功
     */
    @GetMapping("/user/query")
    public ResponseEntity<?> queryUser(
            @RequestParam String username) throws AuthenticationException {

        List<Object> list = new ArrayList<>();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null)
        {
            list.add(authentication.getName());
            list.add(authentication.getDetails());
        }
        list.add(UserDetailsServiceImpl.mapUsers);
        return ResponseEntity.ok(list);
    }

}
